package com.yc.bbs.controller;


import com.yc.bbs.dao.BoardMapper;
import com.yc.bbs.entity.Board;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api")
public class BreadController {
    @Autowired
    BoardMapper boardMapper;
    @RequestMapping("/breadcrumb")
    public List<BreadcrumbItem> getCurrentBreadcrumb(HttpServletRequest request, BreadcrumbItem breadcrumbItem) {
        // 根据请求的路径或其他逻辑来构建面包屑
        List<BreadcrumbItem> breadcrumbItems = new ArrayList<>();

        // 示例：假设你有一个简单的URL到名称的映射
        String currentPath = request.getRequestURI();
        String[] pathParts = currentPath.split("/");
        Board board = boardMapper.find(Integer.valueOf(breadcrumbItem.getBid()));

        // 构造一个简单的面包屑，这里只是一个示例
        for (int i = 1; i < pathParts.length; i++) {
            String name = board.getBoardname(); // 在实际应用中，这里应该从数据库或配置中获取
            String url = "/" + String.join("/", Arrays.copyOfRange(pathParts, 0, i + 1));
            breadcrumbItems.add(new BreadcrumbItem(name, url.equals(currentPath) ? null : url,null));
        }

        // 注意：最后一个项目通常是当前页面，它没有链接
        breadcrumbItems.add(new BreadcrumbItem("Current Page", null,null));

        // 返回除最后一个无链接项之外的所有项（或者根据需求调整）
        return breadcrumbItems.subList(0, breadcrumbItems.size() - 1);
    }

    @Data
   @AllArgsConstructor
    @NoArgsConstructor
    // 简单的BreadcrumbItem类
    public static class BreadcrumbItem {
        private String name;
        private String url;
        private String bid;
        // ...
    }
}
